package com.adu.springbootdemo.controller;

import com.adu.springbootdemo.domain.BatchUser;
import com.adu.springbootdemo.domain.User;
import com.adu.springbootdemo.service.UserService;
import com.adu.springbootdemo.util.JsonResult;
import com.adu.springbootdemo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息控制类
 */
@RestController
public class UserController extends BaseController {
    @Autowired
    UserService userService;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    // 访问方式：http://localhost:8081/findById?id=3
    @GetMapping("/findById")
    public Result findById(@RequestParam("id") int id) {
        User user = userService.findById(id);
        return JsonResult.OK(200, "success", user);
    }

    // 访问方式：http://localhost:8081/findById/3
    @GetMapping("/findById/{id}")
    public Result findById00(@PathVariable("id") int id) {
        User user = userService.findById(id);
        return JsonResult.OK(200, "success", user);
    }



    /**
     * 插入
     * @param user
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody User user){
        userService.save(user);
        return JsonResult.OK(200, "success");
    }

    /**
     * 更新
     * @param user
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody User user){
        userService.update(user);
        return JsonResult.OK(200, "success");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam("id") int id) {
        userService.deleteById(id);
        return JsonResult.OK(200, "success");
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(){
        startPage();
        List<User> userList=userService.findAll();
        return JsonResult.OK(200, "success",userList);
    }

    /**
     * 批量保存
     * @param userList
     * @return
     */
    @PostMapping("/batchSave")
    public Result batchSave(@RequestBody BatchUser userList){
        userService.batchSave(userList);
        return JsonResult.OK(200, "success");
    }
}
